﻿/*
 * @Autor: PK
 * @Date: 2021/12/25 11:11:SS
 */

using System.Collections.Generic;
using System.Linq;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Interfaces;
using ShkSimulation.core.optimization.variable;

namespace ShkSimulation.core.optimization.algorithm.classicGeneticAlgotithm {

	public class GeneticAlgorithmVariableAdapter {
		
		public List<string> NameKeys { get; set; }

		public GeneticAlgorithmVariableAdapter(List<string> nameKeys) {
			this.NameKeys = nameKeys;
		}

		public GeneticAlgorithmVariableAdapter(DecisionVariableVector<double> variableValues) {
			NameKeys = variableValues.Select(each => each.Name).OrderBy(each => each).ToList();
		}

		public DecisionVariableVector<double> Convert(IChromosome chromosome) {
			Dictionary<string, double> namedValue = new Dictionary<string, double>();
			var temp = ((VectorChromosome<double>)chromosome);
			for (int i = 0; i < temp.Length; i++) {
				namedValue.Add(NameKeys[i], temp[i]);
			}
			return new DecisionVariableVector<double>(namedValue, 0, namedValue.Values.Max());
		}

	}

}